<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta charset="utf-8" />
<meta name="description" content="Mailbox with some customizations as described in docs" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<%@ include file="/include/main_include.jsp"%>
</head>
<body class="easyui-layout">

 <div data-options="region:'west',title:'城市列表'" style="width: 15%; background: #fafafa;">
		<ul id="cityTree"></ul>
	</div>
	<div data-options="region:'center',border:false" style="width: 100%;">
		<div class="easyui-layout" data-options="fit:true">
			<div data-options="region:'north',title:'合作伙伴',border:false,collapsible:false" style="height: 100%">
				<div data-options="region:'center',border:false" style="height: 100%">
					<table id="agentTable"></table>
				</div>
			</div>
		</div>
	</div>
	<script type="text/javascript">
		var searchParams;
		var cityTree;
		var agentTable;
		var agentUserTable;

		$(function(){
			
			cityTree = $('#cityTree').tree({
				url : rootpath + '/hhCity/cityList.do?id=0',
				animate : true,
				onBeforeLoad : function(node, param) {
					$.uitools.showLoading();// 打开处理中遮罩层
				},
				onLoadSuccess : function(node, data) {
					$.uitools.hideLoading();// 关闭处理中遮罩层
				},
				onLoadError : function(arguments) {
					$.uitools.hideLoading();// 关闭处理中遮罩层
				},
				onClick : function(node) {
					agentTable.treegrid({
						url : rootpath + '/hhAgent/agentList.do?countyId=' + node.id
					});
				}
			});

			//代理商
			  agentTable = $('#agentTable').treegrid({
				fitColumns : true,
				fit : true,
				nowrap : true,
				animate : true,
				nowrap : true,
				idField : 'id',
				treeField : 'text',
				columns : [ [  
					{field:'name',title:'名称',width:'10%',sortable:true,align:'center'},
					{field:'province',title:'省',width:'10%',sortable:true,align:'center'},
					{field:'city',title:'市',width:'10%',sortable:true,align:'center'},
					{field:'county',title:'县',width:'10%',sortable:true,align:'center'},
					{field:'agentFee',title:'代理费',width:'10%',sortable:true,align:'center'},
					{field:'balance',title:'余额',width:'10%',sortable:true,align:'center'},
					{field:'mobile',title:'电话',width:'10%',sortable:true,align:'center'},
					{field:'address',title:'地址',width:'10%',sortable:true,align:'center'},
					{field:'addTime',title:'添加时间',width:'10%',sortable:true,align:'center',formatter:function(value,row,index){
	                	return timestampToTime(value);
	    			   }},
					{field:'bankCard',title:'银行卡号',width:'10%',sortable:true,align:'center'},
					{field:'bankName',title:'开户人',width:'10%',sortable:true,align:'center'},
					{field:'bank',title:'开户行',width:'10%',sortable:true,align:'center'},
					{field:'bankBranch',title:'开户支行',width:'10%',sortable:true,align:'center'}
				] ],
				onClickRow : function(rowIndex ) {
					agentUserTable.datagrid({
						url : rootpath + '/sysUser/userList.do?agentId=' + rowIndex.id
					});
				}
			});
	//代理商用户id
			  agentUserTable = $('#agentUserTable').datagrid(BasicData.createDatagrid({
					autoWidth : true,
					idField : 'USER_ID',
					columns : [ [  
						{field:'parentid',title:'父级编码',width:'10%',sortable:true,align:'center'},
						{field:'title',title:'名称',width:'10%',sortable:true,align:'center'},
						{field:'level',title:'级别',width:'10%',sortable:true,align:'center'},
						{field:'parentName',title:'父级名称',width:'10%',sortable:true,align:'center'},
						{field:'rootName',title:'省名称',width:'10%',sortable:true,align:'center'}
					] ],
					onDblClickRow : function(rowIndex, rowData) {
					},
					onClickRow : function(rowIndex, rowData) {
					}
				}));

			//添加按钮事件
			$('#agentAddBtn').on('click', function() {
				openedit('');
			});
			//修改按钮事件
			$('#agentEditBtn').on('click', function() {
				var node = agentTable.datagrid('getSelected');
				if (node) {
					openedit(node.agentId);
				} else {
					layer.msg('请选择一条数据！', {
						time : 1000
					})
				}
			});
		});
	 
		//打开查询窗体
		function opensearch(){
			layer.open({
				title : '查询条件',
				type : 2,
				btn : ['查询','重置'],
				yes : function(index,layero) {
					var iframeWin = window[layero.find('iframe')[0]['name']];
					searchParams = iframeWin.$("#searchForm").serialize();
					var newparam = iframeWin.$("#searchForm").serializeJSON();
					$.extend(newparam,dataTable.queryParams);
					$('#data_table').datagrid('load',newparam);
					layer.close(index);
				},
				btn2 : function(index, layero) {
					var iframeWin = window[layero.find('iframe')[0]['name']];
					iframeWin.$("#searchForm").form("clear");
					return false;
				},
				area : [ '80%', '80%' ],
				content : rootpath + '/hhAgent/toSearch.do?'+searchParams
			});
		}
		//编辑打开窗体
		function openedit(agentId){
			layer.open({
				title : '信息',
				type : 2,
				btn : ['保存','关闭'],
				yes : function(index,layero) {
					var iframeWin = window[layero.find('iframe')[0]['name']];
					iframeWin.$("#dataForm").submit();
				},
				btn2 : function(index, layero) {
				},
				area : [ '80%', '80%' ],
				content : rootpath + '/hhAgent/toEdit.do?agentId='+agentId
			});
		}
		//删除
		function del() {
			var node = dataTable.datagrid('getSelected');
			if (node) {
				layer.confirm('您确定要删除吗？', {
					btn : [ '确定', '取消' ]
				}, function(index, layero) {
				    layer.close(index);
					$.uitools.showLoading();//打开处理中遮罩层
					$.ajax({
						cache : true,
						type : "POST",
						url : rootpath + '/hhAgent/delete.do',
						data : {
							agentId : node.agentId
						},
						error : function(request) {
							$.uitools.hideLoading();//关闭处理中遮罩层
							layer.msg('系统异常！', {
								icon : 2,
								time : 1000
							})
						},
						success : function(data) {
							$.uitools.hideLoading();//关闭处理中遮罩层
							if (data.code==0){
								layer.msg('删除成功！', {
									icon : 1,
									time : 1000
								}, function() {
									dataTable.datagrid('reload');
								})
							}else {
								layer.msg('删除异常！', {
									icon : 2,
									time : 1000
								})
							}
						}
					});
				}, function() {
				});
			} else {
				layer.msg('请选择一条数据！', {
					time : 1000
				})
			}
		}
	</script>
</body>
</html>